
//--> Loth 2011 
//--> map systeme based on swf file see asset/map/

package chamans.away
{
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.filters.*;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Matrix3D;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	import flash.ui.Keyboard;
	
	
	public class map extends Sprite {  
		
		private var MapSize:int=2048;
		private var MidSize:int;
		
		private var ech1:Number;
		private var ech2:Number;
		public var CamCatch:Boolean=true;
		
		private var BASE:Object;
		public var Plan2d:MovieClip;
		private var onSTAGE:Boolean;
		private var MAP:Sprite=new Sprite();
		
		private var Pbit:Bitmap;
		public var mapdata:BitmapData ;
		public var mapHeight:BitmapData;
		public var mapDistrib:BitmapData;
		private var V:Point=new Point (0,0);
		private var pp:Point=new Point(0,0), prev:Point=new Point(0,0), offset:Point=new Point(0,0);
		private var speed:Point=new Point(0,0), D1:Point=new Point(0,0), D2:Point=new Point(0,0);
		public var sm:Number=.7, Angle:int, Angle2:int, posY:int;
		
		private var PlayerPos:Point;
		private var M:Object;
		private var mouseOffStage:Boolean=false;
		private var rt:Boolean=false;
		public var decalY:int=0;
		private var decalold:int=0;
		public var ROTATION:Boolean=false, MOVE:Boolean=false, FIRST:Boolean=true;
		
		public function map(base:Object, clip:MovieClip, pos:Vector3D, INFO:Vector3D, floor:BitmapData=null):void
		{
			ech1=INFO.x;
			ech2=INFO.y;
			sm=pos.z;
			BASE=base; 
			PlayerPos=new Point(pos.x, pos.y)
			Plan2d=clip;
			addEventListener(Event.ADDED_TO_STAGE, INIT, false, 0, true); 
			
			if(floor!=null){
				var bitr:Bitmap=new Bitmap(floor)
				bitr.scaleX=2
				bitr.scaleY=2
				Plan2d.Height.addChild(bitr)
			}
			
		}
		private function INIT(e:Event):void {
			
			removeEventListener(Event.ADDED_TO_STAGE, INIT);
			
			MidSize=MapSize>>1;
			
			MAP=new Sprite()
			if(Plan2d.wall){ 
				mapdata=BASE.TOOL.bit(Plan2d.wall, MapSize, MapSize, 1, true);
				Plan2d.wall.visible=false;
			}
			if(Plan2d.Height){ 
				mapHeight=BASE.TOOL.bit(Plan2d.Height, MapSize, MapSize, 1, false);
				Plan2d.Height.alpha=.25
			}
			if(Plan2d.Hmap){ 
				mapDistrib=BASE.TOOL.bit(Plan2d.Hmap, MapSize, MapSize, 1, true);
				Plan2d.Hmap.visible=false;
			}
			
			//	color(View2d.line, 0x556600,1)
			MAP.x=-0; MAP.y=0;
			
			Plan2d.Height.scaleX=Plan2d.Height.scaleY=ech2;
			Plan2d.Height.x=Plan2d.Height.y=-MidSize;
			
			Plan2d.line.scaleX=Plan2d.line.scaleY=ech2;
			Plan2d.line.x=Plan2d.line.y=-MidSize;
			MAP.addChild(Plan2d);
			
			addChild(MAP);
			createPlayerSprite();
		}
		
		private function createPlayerSprite():void {
			var size:int=30//4//8//6
			var c:int=0x323436
			var c2:int=0xDADED3
			var a:Number=1//.3
			var Dbit:BitmapData=BASE.TOOL.bit(cercle( c,1, size, size), size, size, 1, true)
			//var Dbit:BitmapData=O.TOOL.bit(carre( c,1, size, size), size, size, 1, true)
			Pbit=new Bitmap(Dbit);
			
			// decor cercle
			var bordure:Sprite=new Sprite()	
			var axe:Sprite=carreL( c, a, size, size, size>>1, size>>1);
			var ax1:Shape=line(0,-50, c, a, 0, -4);
			var ax2:Shape=line(0,-50, c, a, 0, -4);
			var arc:Shape=ARC(1000/ech1, c, a);
			var d0:Shape=cercle( c, a, 92,92,4,4,true, 1);
			var d3:Shape=cercle( c2, .2, 88,88,6,6,true, 5);
			var d2:Shape=cercle( c, a, 82,82,9,9,true, 1);
			var zone:Shape=cercle( c, 1, 92, 92, -46, -46);
			ax1.rotation=45; ax2.rotation=-45;
			axe.addChild(ax1);axe.addChild(ax2);axe.addChild(arc);
			
			bordure.addChild(d3);
			bordure.addChild(d0);
			bordure.addChild(d2);
			bordure.addChild(axe);
			//MODE(bordure)
			axe.x=50; axe.y=50;
			var Cbit:BitmapData=BASE.TOOL.bit(bordure, 100, 100, 1, true)
			var decor:Bitmap=new Bitmap(Cbit)
			
			offset= new Point(size>>1, size>>1)
			pp= new Point(MidSize+(PlayerPos.x/ech1), MidSize+(-PlayerPos.y/ech1));
			Pbit.x=-offset.x; Pbit.y=-offset.y;
			MAP.addChild(zone)
			MAP.addChild(decor);decor.x=-50; decor.y=-50
			Plan2d.mask=zone
			
			posY=GETZ()
			decalY = BASE.view.camera.y
			
			rotate()
			movePlayer()
			MAP.mouseEnabled=MAP.mouseChildren=false
		}
		
		public function update(e:Event=null):void {
			GETKEYS()
			if(MOVE || FIRST) movePlayer()
		}
		public function rotate(e:Event=null):void {
			
			Plan2d.rotation=-BASE.Camera.M.V-90	
		}
		
		private function GETKEYS(e:Event=null):void {	
			MOVE=false
			
			if( BASE.KEY.isDown( Keyboard.LEFT)) { speed.y+=sm; MOVE=true }
			if( BASE.KEY.isDown( 65 )){ speed.y+=sm; MOVE=true }// A
			if( BASE.KEY.isDown( 81 )){ speed.y+=sm; MOVE=true }// Q
			
			if( BASE.KEY.isDown( Keyboard.RIGHT)) { speed.y-=sm; MOVE=true }
			if( BASE.KEY.isDown( 68 )) { speed.y-=sm; MOVE=true  }// D
			
			if( BASE.KEY.isDown( Keyboard.UP)) { speed.x+=sm; MOVE=true }
			if( BASE.KEY.isDown( 90 )) { speed.x+=sm; MOVE=true }// Z
			if( BASE.KEY.isDown( 87 )) { speed.x+=sm; MOVE=true }// W
			
			if( BASE.KEY.isDown( Keyboard.DOWN ))  { speed.x-=sm; MOVE=true }
			if( BASE.KEY.isDown( 83 )) { speed.x-=sm; MOVE=true }// S
			
			if( BASE.KEY.isDown( Keyboard.PAGE_UP))  { decalY+=(sm*ech1); MOVE=true }
			if( BASE.KEY.isDown( Keyboard.PAGE_DOWN))  { decalY-=(sm*ech1); MOVE=true;  }
			BASE.SYS.cible_walk(MOVE)
		}
		
		private function movePlayer():void
		{
			//HEIGHT MAP
			posY = GETZ()
			
			//COLLISION
			if (Pbit.bitmapData.hitTest( new Point(pp.x-offset.x, pp.y-offset.y),255, mapdata,new Point(0,0)))
			{ pp=prev; } else {  
				prev=new Point(pp.x, pp.y)
				
				Angle=BASE.Camera.M.V+90//M.V+90
				V.x=Math.cos(RadDeg(Angle))
				V.y=Math.sin(RadDeg(Angle))
				D1.y = Number((V.x*speed.x).toFixed(1)); 
				D1.x = Number((V.y*speed.x).toFixed(1));
				D2.x = Number((V.x*speed.y).toFixed(1)); 
				D2.y = Number((V.y*speed.y).toFixed(1));
				pp.x +=D1.x ;  pp.y -=D1.y
				pp.x -=D2.x ;  pp.y -=D2.y
				
				//view 2d move
				with(Plan2d){
					line.x = Height.x = ((MidSize-pp.x)-MidSize)*ech2; 
					line.y = Height.y = ((MidSize-pp.y)-MidSize)*ech2;
				}
				if(speed.x!=0 || speed.y!=0 ){
					if (Math.abs(speed.x)<1) speed.x=0
					if (Math.abs(speed.y)<1) speed.y=0	
					MoveCamera()
					speed=new Point(0,0);
				}}	
			if(decalold!=decalY) MoveCamera();
			if(FIRST)FIRST=false; 
			decalold=decalY;
		}
		private function GETZ():int {
			var n:int=((mapHeight.getPixel(pp.x, pp.y) & 0xff)*10)+decalY;
			return n;
		}
		private function MoveCamera():void {
			if(CamCatch){
				BASE.Camera.move(new Vector3D( ((pp.x-MidSize)*ech1)>>0, posY, ((MidSize-pp.y)*ech1)>>0 ))
				//    BASE.SYS.OUT("Move"+O.Camera.M.baseTarget+ " rot"+O.Camera.M.V+" "+ O.Camera.M.H)
			}}
		
		
		//__________________________________MATH / GRAPHISME_______________________________\\
		private function cercle( c:int, a:Number=1, w:int=100, h:int=100, x:int=0, y:int=0, t:Boolean=false, l:int=0):Shape {
			var g:Shape=new Shape
			if(t)  {g.graphics.lineStyle(l, c, a);g.graphics.beginFill(c,0)}
			else g.graphics.beginFill(c,a)
			g.graphics.drawEllipse(x,y,w,h)
			if(!t)  g.graphics.endFill()
			return g
		}
		private function line(s:int, h:int, c:int, a:Number=1, x:int=0,y:int=0):Shape {
			var g:Shape=new Shape
			g.graphics.lineStyle(s, c, a)
			g.graphics.moveTo(x,y)
			g.graphics.lineTo(0,h)
			return g
		}
		private function carre( c:int=0x00, a:Number=1, w:int=1, h:int=1, x:int=0, y:int=0):Shape { 
			var g:Shape=new Shape; 
			g.graphics.beginFill(c,a); g.graphics.drawRect(x,y,w,h); g.graphics.endFill()
			return g  
		}
		private function carreL( c:int=0x00, a:Number=1, w:int=1, h:int=1, x:int=0, y:int=0):Sprite { 
			var g:Sprite=new Sprite; 
			g.graphics.clear()
			g.graphics.lineStyle(0, c, a);
			g.graphics.beginFill(c, 0);
			g.graphics.drawRect(-x,-y,w,h);
			return g  
		}
		private function ARC(r:int=400, c:int=0xff0000, a:Number=1, AD:int=-135, AF:int=90, x:int=0, y:int=0, S:int=30):Shape{
			var pas:Number = 360/(S*2); 
			var inc:Number = AD; 
			var g:Shape=new Shape()
			g.graphics.lineStyle(1, c, a)
			g.graphics.moveTo (x+r*Math.cos(inc*Math.PI/180),y+r*Math.sin(inc*Math.PI/180))        
			while(inc<=AD+AF) { 
				inc+=pas; g.graphics.lineTo (x+r*Math.cos(inc*Math.PI/180),y+r*Math.sin(inc*Math.PI/180)); 
				inc+=pas; g.graphics.moveTo (x+r*Math.cos(inc*Math.PI/180),y+r*Math.sin(inc*Math.PI/180)); 
			}
			g.graphics.endFill()
			return g
		}
		public function DegRad(r:Number):Number{  return(r * (180 / Math.PI)) }
		private function RadDeg(d:Number):Number{ return(d * (Math.PI / 180)) }
		public function color( o:*, c:int, a:Number=1 ):void{
			var nc:ColorTransform = o.transform.colorTransform
			nc.color = c; nc.	alphaMultiplier = a; o.transform.colorTransform = nc
		}
	}}